package com.tencent.tsf.demo.zuul.filter;

import com.netflix.zuul.exception.ZuulException;
import com.tencent.tsf.gateway.core.annotation.TsfGatewayFilter;
import com.tencent.tsf.gateway.zuul1.filter.TsfGatewayZuulFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;

import java.util.List;

/**
 * @author liweic
 * @title: TestTsfFilter
 * @description: TestTsfFilter
 * @date 2020/12/16 11:13
 */
@TsfGatewayFilter
public class TestTsfFilter extends TsfGatewayZuulFilter {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestTsfFilter.class);

    @Autowired
    private DiscoveryClient discoveryClient;

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 100;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("hello world");
        LOGGER.info("hello world,02");

        System.out.println("==================================================");
        LOGGER.info("discoveryClient:" + discoveryClient);
        discoveryClient.getServices().forEach(System.out::println);
        System.out.println("==================================================");
        List<ServiceInstance> list = discoveryClient.getInstances("provider-demo");
        list.forEach(System.out::println);
        System.out.println("==================================================");

        return null;
    }
}
